/*
 * ThaiSegRun.java
 * 
 * Created on 02-Jun-2007, 05:22:11
 */

package net.siit.ling.exe;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Collections;
import java.util.List;

import net.siit.ling.*;
import net.siit.datatype.AnnotatedRange;

/**
 *
 * @author arthit
 */
public class ThaiSegRun {
    /** Creates a new instance of ThaiSegRun
     */
    public ThaiSegRun() {
        
    }

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException 
     * @throws java.io.UnsupportedEncodingException 
     * @throws java.io.IOException 
     * @throws java.lang.Exception 
     */
    public static void main(String[] args) {
        ThaiSeg seg = null;
        try {
            seg = new ThaiSeg(new URL("file:///home/arthit/corpus/md/yaitron-md-dict.txt"), "utf-8");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (seg == null) {
            System.err.println("Cannot load tokeniser. Dictionary path is " + ThaiSeg.DEFAULT_DICTIONARY_URL);
            System.exit(1);
        }

        String delimiter = args[0];
        String inFileCharset = args[1];
        String inFilename    = args[2];
        
        FileInputStream inFile = null;
        BufferedReader reader  = null;
        
        try {
            inFile = new FileInputStream(inFilename);
        } catch(FileNotFoundException ex) {
            ex.printStackTrace();
        }
        if (inFile == null) {
            System.err.println("Cannot open file.");
            System.exit(2);
        }
        
        try {
            String line;
            reader = new BufferedReader(new InputStreamReader(inFile, inFileCharset));
            while ((line = reader.readLine()) != null) {
                System.err.println("Original : " + line);

                List<AnnotatedRange<Integer>> ranges = null;
                try {
                    ranges = seg.tokenize(line, ThaiSeg.LONGEST_MATCH);
                } catch(Exception ex) {
                    ex.printStackTrace();
                    System.exit(4);
                }
                if (ranges.size() > 1) {
                    Collections.sort(ranges);   // sort

                    System.err.print  ("Segmented: ");
                    for (AnnotatedRange r : ranges) {
                        System.out.print(line.substring(r.getStart(), r.getEnd()));
                        System.out.print(delimiter);
                        //System.out.print(r.getLabel());
                    }
                }
                
                System.out.println();
            }
        } catch (UnsupportedEncodingException ex) {
            System.err.println("Unsupported character set " + inFileCharset);
            System.exit(3);
        } catch (IOException ex) {
            System.err.println("Error while reading file.");
            System.exit(2);
        }

        try {
            reader.close();
            inFile.close();
        } catch (IOException ex) {
            // nothing
        }
    }

}
